package com.shqcjd.yunweiyanoperator.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.ui.activity.DepartmentSelectActivity;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.shqcjd.yunweiyanoperator.util.UIUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class AddStaffDialog extends Dialog {
    private Button btn_dialog_add;
    private EditText et_dialog_staff_name, et_dialog_staff_phone, et_dialog_staff_password;
    private Context context;
    private setOnClickDialogListener listener;
    private TextView tv_department;
    private TagFlowLayout flowlayout_specialty;
    private ArrayList<String> mTypeVals = new ArrayList<>();
    public void setOnClickDialogListener(setOnClickDialogListener listener){
        this.listener = listener;
    }

    public AddStaffDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public AddStaffDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected AddStaffDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_staff_dialog);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        btn_dialog_add = findViewById(R.id.btn_dialog_add);
        et_dialog_staff_name = findViewById(R.id.et_dialog_staff_name);
        et_dialog_staff_phone = findViewById(R.id.et_dialog_staff_phone);
        et_dialog_staff_password = findViewById(R.id.et_dialog_staff_password);
        tv_department = findViewById(R.id.tv_department);
        flowlayout_specialty = findViewById(R.id.flowlayout_specialty);

        btn_dialog_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = et_dialog_staff_name.getText().toString().trim();
                String phone = et_dialog_staff_phone.getText().toString().trim();
                String password = et_dialog_staff_password.getText().toString().trim();
                if(!name.equals("")&&!phone.equals("")&&!password.equals("")&&flowlayout_specialty.getSelectedList().size()>0){
                    if(password.length()>=6){
                        if (UIUtils.isMobile(et_dialog_staff_phone.getText().toString())) {
                            if(listener!=null){
                                listener.onClickAdd(name, phone, password);
                            }
                        } else {
                            ToastUtils.showShort(context, "请输入正确的手机号");
                        }
                    }else{
                        ToastUtils.showShort(context, "密码必须大于等于6位");
                    }
                }else{
                    ToastUtils.showShort(context, "请确认所有信息是否填写完成");
                }
            }
        });

        tv_department.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onAddDepartment();
                }
            }
        });

    }

    public void setStr(String str){
        tv_department.setText(str);
    }


    //设置流标签
    public void setTagView(List<String> list){
        if (flowlayout_specialty == null) {
            return;
        }
        mTypeVals.clear();
        mTypeVals.addAll(list);
        TagAdapter tagAdapter = new TagAdapter<String>(mTypeVals.toArray(new String[mTypeVals.size()])) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.tv_tag,
                        flowlayout_specialty, false);
                tv.setText(s);
                return tv;
            }

        };
        flowlayout_specialty.setAdapter(tagAdapter);
    }

    //获取选择的专业id
    public Set<Integer> getTagIds() {
        if (flowlayout_specialty == null) {
            return null;
        }
        return flowlayout_specialty.getSelectedList();
    }

    public void cleanData(){
        et_dialog_staff_name.setText("");
        et_dialog_staff_phone.setText("");
        et_dialog_staff_password.setText("");
        tv_department.setText("请选择部门 >");
    }

    public interface setOnClickDialogListener{
        void onClickAdd(String name, String phone, String password);
        void onAddDepartment();
    }
}
